﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeltaPlcSystem
{
    public partial class PlcForm : Form
    {
        public delegate void AddDele(List<string> tempData,int mode);
        public event AddDele AddEvent;
        int mode = 0;
        public PlcForm()
        {
            InitializeComponent();
            MaximizeBox = false;

            uiComboBox1.Items.Add("AsAh-Series");
            uiComboBox1.Items.Add("Dvp-Series");
            uiComboBox1.Items.Add("HMI");

            uiComboBox1.SelectedIndex = 0;


            uiComboBox2.Items.Add("-");
            for (int i = 0; i < Form1.MachineList.Count; i++)
            {
                uiComboBox2.Items.Add(Form1.MachineList[i].MachineName);
            }
            uiComboBox2.SelectedIndex = 0;
        }


        public PlcForm(int model) : this()
        {
            mode = model;
            if (model == 0)
            {
                uiButton1.Text = "保存";
                Text = "添加设备";

            
                Txt_client_IP.Text = string.Empty;

                Txt_interfaceID.Text = string.Empty;


                Txt_equipType.Text = string.Empty;


                Txt_status.Text = "D9020";


                Txt_statusCode.Text = "D9022";


                Txt_passQty.Text = "D9024";


                Txt_failQty.Text = "D9026";


                Txt_errorCnt.Text = "D9028";


                Txt_errorTimes.Text = "D9030";


                Txt_cycleTime.Text = "D9032";


                Txt_runningTime.Text = "D9034";


                Txt_waitingTime.Text = "D9036";


                Txt_selfCheck.Text = "D9040";

                Txt_inputQty.Text = "D9038";


                Txt_barcode.Text = "D9010";


                Txt_model.Text = "D9000";


                Txt_CT_Q.Text = "D9044";


                Txt_CT_M.Text = "D9042";

                uiTextBox2.Text = "1";

                uiTextBox1.Text = string.Empty;


                uiRadioButton2.Checked = true;
            }
        }


        public PlcForm(int model, DeviceClass dc) : this()
        {
            mode = model;
            if (model == 1)
            {
                uiButton1.Text = "更新";
                Text = "更新设备";

          

                Txt_client_IP.Text = dc.Ip;
                Txt_client_IP.ReadOnly = true;

                Txt_interfaceID.Text = dc.InterfaceID;
              

                Txt_equipType.Text = dc.EquipType;
              

                Txt_status.Text = dc.Status;
         

                Txt_statusCode.Text = dc.StatusCode;
             

                Txt_passQty.Text = dc.PassQty;
              

                Txt_failQty.Text = dc.FailQty;
              

                Txt_errorCnt.Text = dc.ErrorCnt;
             

                Txt_errorTimes.Text = dc.ErrorTimes;
           

                Txt_cycleTime.Text = dc.CycleTime;
              

                Txt_runningTime.Text = dc.RunningTime;
              

                Txt_waitingTime.Text = dc.WaitingTime;
             

                Txt_selfCheck.Text = dc.SelfCheck;
              
                Txt_inputQty.Text = dc.InputQty;
              

                Txt_barcode.Text = dc.Barcode;
            

                Txt_model.Text = dc.Model;
              

                Txt_CT_Q.Text = dc.CT_Q;
              

                Txt_CT_M.Text = dc.CT_M;
             

            
                uiTextBox1.Text = dc.EquipName;

                try
                { uiComboBox1.SelectedIndex = int.Parse(dc.PlcMode); }
                catch { }

                uiTextBox2.Text = dc.ModulusValue.ToString();


                uiComboBox2.SelectedItem = dc.VirtualMachine;


                if (dc.IsTimeEnable)
                {
                    uiRadioButton1.Checked = true; uiRadioButton2.Checked = false;
                }
                else
                { uiRadioButton1.Checked = false; uiRadioButton2.Checked = true; }
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
           
                if (Txt_client_IP.Text == null || Txt_client_IP.Text == null || Txt_client_IP.Text == null || Txt_client_IP.Text == null || Txt_client_IP.Text == null || Txt_client_IP.Text == null)
                {
                    MessageBox.Show("IP，服务器，十码不能为空");
                    return;
                }


                if (!Txt_client_IP.Text.Contains("."))
                {
                    MessageBox.Show("IP有误");
                    return;
                }
                else
                {
                    if (Txt_client_IP.Text.Split('.').Length != 4)
                    {
                        MessageBox.Show("IP有误");
                        return;
                    }


                if (int.Parse(Txt_client_IP.Text.Split('.')[3])>254|| int.Parse(Txt_client_IP.Text.Split('.')[3])==0)
                {
                    MessageBox.Show("IP最后位不能超过254且不能为0");
                    return;
                }
            }

                List<string> list = new List<string>();
                list.Add(Txt_client_IP.Text);
                list.Add(Txt_interfaceID.Text);
                list.Add(uiTextBox1.Text);
                list.Add(Txt_equipType.Text);
                list.Add(Txt_status.Text);
                list.Add(Txt_statusCode.Text);
                list.Add(Txt_passQty.Text);
                list.Add(Txt_failQty.Text);

                list.Add(Txt_errorCnt.Text);
                list.Add(Txt_errorTimes.Text);
                list.Add(Txt_cycleTime.Text);

                list.Add(Txt_runningTime.Text);
                list.Add(Txt_waitingTime.Text);
                list.Add(Txt_selfCheck.Text);

                list.Add(Txt_inputQty.Text);
                list.Add(Txt_barcode.Text);
                list.Add(Txt_model.Text);

                list.Add(Txt_CT_Q.Text);
                list.Add(Txt_CT_M.Text);
                list.Add(uiComboBox1.SelectedIndex.ToString());
                list.Add(uiTextBox2.Text.ToString());
            
            list.Add(uiComboBox2.SelectedItem.ToString());

            if (uiRadioButton1.Checked)
            {
                list.Add("1");
            }
            else
                list.Add("0");


            AddEvent(list,mode);


         
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "文档(*.txt)|*.txt";
                dialog.InitialDirectory = Application.StartupPath + @"\Template";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    string[] dataStr = File.ReadAllLines(dialog.FileName);

                    foreach (string item in dataStr)
                    {
                        if (item.Split(':')[0].Trim().ToLower() == "status")
                        {
                            Txt_status.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "statuscode")
                        {
                            Txt_statusCode.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "passqty")
                        {
                            Txt_passQty.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "failqty")
                        {
                            Txt_failQty.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "errorcnt")
                        {
                            Txt_errorCnt.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "errortimes")
                        {
                            Txt_errorTimes.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "cycletime")
                        {
                            Txt_cycleTime.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "runningtime")
                        {
                            Txt_runningTime.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "waitingtime")
                        {
                            Txt_waitingTime.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "selfcheck")
                        {
                            Txt_selfCheck.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "inputqty")
                        {
                            Txt_inputQty.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "ct_m")
                        {
                            Txt_CT_M.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "ct_q")
                        {
                            Txt_CT_Q.Text = item.Split(':')[1].Trim();
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "barcode")
                        {
                            try
                            { Txt_barcode.Text = item.Split(':')[1].Trim(); }
                            catch { }
                        
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "model")
                        {
                            try
                            { Txt_model.Text = item.Split(':')[1].Trim(); }
                            catch { }
                          
                        }
                        else if (item.Split(':')[0].Trim().ToLower() == "modulusvalue")
                        {
                            try
                            {  uiTextBox2.Text = item.Split(':')[1].Trim(); }
                            catch { }

                        }
                    }



                    MessageBox.Show("导入成功");
                }
            }
            catch { MessageBox.Show("模版异常，导入失败"); }

        }
    }
}
